<?php

namespace app\merchant\controller\wxapp;

use app\merchant\controller\Base;
use app\merchant\model\WxappHelp as WxappHelpModel;

/**
 * 小程序帮助中心
 * Class help
 * @package app\store\controller\wxapp
 */
class Help extends Base
{
    /**
     * 帮助中心列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new WxappHelpModel;
        $list = $model->getList();
        return $this->renderSuccess($list, "查询成功");
    }

    /**
     * 添加帮助
     * @return array|mixed
     */
    public function add()
    {
        $model = new WxappHelpModel;

        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }


    /**
     * 帮助详情
     * @return array|mixed
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['help_id']) || isInt($param['help_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = WxappHelpModel::detail($param['help_id']);
        if(!$model){
            return $this->renderError([],"该帮助不存在，请刷新重试");
        }

        return $this->renderSuccess($model,"查询成功");
    }


    /**
     * 更新帮助
     * @param $help_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        if(!isset($param['help_id']) || isInt($param['help_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = WxappHelpModel::detail($param['help_id']);
        if(!$model){
            return $this->renderError([],"该帮助不存在，请刷新重试");
        }

        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '编辑失败');
    }

    /**
     * 删除帮助
     * @param $help_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['help_id']) || isInt($param['help_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = WxappHelpModel::detail($param['help_id']);
        if(!$model){
            return $this->renderError([],"该帮助不存在，请刷新重试");
        }
        if (!$model->remove()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}
